package cn.sample.moreitems.item;

import cn.sample.moreitems.entity.ThrownRockEntity;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;

public class ThrownRockItem extends Item {
    public ThrownRockItem() { super(new Item.Properties().stacksTo(64)); }

    @Override
    public InteractionResultHolder<ItemStack> use(Level level, Player player, InteractionHand hand) {
        ItemStack held = player.getItemInHand(hand);
        level.playSound(null, player.getX(), player.getY(), player.getZ(),
                SoundEvents.SNOWBALL_THROW, SoundSource.NEUTRAL,
                3F, 0.4F / (level.getRandom().nextFloat() * 0.4F + 0.8F));

        if (!level.isClientSide) {
            ThrownRockEntity rock = new ThrownRockEntity(level, player);
            rock.shootFromRotation(player, player.getXRot(), player.getYRot(),
                    0.0F, 1.5F, 1.0F);   // 1.5F 为初速度
            level.addFreshEntity(rock);
        }

        if (!player.getAbilities().instabuild) {
            held.shrink(1);
        }
        return InteractionResultHolder.sidedSuccess(held, level.isClientSide());
    }
}
